String in C

The sequence of characters enclosed within double quotes is called as string. In C language the string is always terminated by null character \0.

Though C language does not contain string datatype we can use char array to get the string from the user.

Example

char name[] = "Andrew";

Now the characters will be stored in the following index positions starting from 0. The null character \0 will be in the last index.
A - 0, n - 1, d - 2, r - 3, e - 4 , w - 5

It is possible to provide size to an array and initialize the variable.

char name[13] = "ReadersBuddy";

In order to get the string as input we can use %s format specifier inside the scanf() function.

Example

char str[20];
printf("Enter a string\n");
scanf("%s", &str); 

Output

Enter a string
Andrew

The scanf() function, terminates its input on the first white space it encounters. To get the characters including whitespace and terminate when encountering new line we can use edit set conversion code %[^\n].

Example

char str[20];
printf("Enter a string");
scanf("%[^\n]", &str); 
printf("%s", str);

Now until it encouter a new line character(pressing enter key) we can get and store the value in str variable.

The gets() function is also used to get string as input.

char name[20];
gets(name);
printf("%s", name);

String Functions

There are few functions available in C language to perform string operations. To use those string functions we need to import the string.h header file.

The following are few of the string functions provided by string.h header file

strcat(), strcpy(), strcmp(), strlen(), strrev().

strcat() function

The strcat() function is used to concatenate(combine) two strings.

Example

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[10] = "Andrew";
     char s2[10] = " Rayan";
     strcat(s1,s2);
     printf("Output string after concatenation: %s", s1);
     return 0;
}

Output

Andrew Rayan

In the above program strcat() concatenates the s1 and s2 and save the result string in s1.

strcpy() function

The strcpy() function copies the second string argument to the first string argument.

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[30] = "Andrew";
     char s2[30] = " Rayan";
     /* This function has copied s2 into s1*/
     strcpy(s1,s2);
     printf("String s1 is: %s", s1);
     return 0;
}

Output

String s1 is: Andrew Rayan

strcmp() function

The strcmp() function is used to compare two strings.

Example

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[20] = "ReadersBuddy";
     char s2[20] = "ReadersBuddy.com";
     if (strcmp(s1, s2) ==0)
     {
        printf("string 1 and string 2 are equal");
     }else
      {
         printf("string 1 and 2 are different");
      }
     return 0;
}

Output

string 1 and 2 are different

strlen() function

The strlen() function is used to show the length of a string.

#include <stdio.h>
#include <string.h>
int main()
{
     char str[20] = "ReadersBuddy";
     printf("Length of string is: %d", strlen(str));
     return 0;
}

Output

Length of string is:13

strrev() function

The strrev() function is used to show the reverse of a string.

#include <stdio.h>

int main()
{ 
    char s1[50]; 
  
    printf("Enter your string: "); 
    gets(s1);  
    printf("\nYour reverse string is: %s",strrev(s1)); 
    return 0; 
}

Output

Enter your string: ReadersBuddy
Your reverse string is:yddubsredaeR

Most Read